package com.solome.app;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.solome.base.AbstractScreen;
import com.solome.core.instr.SECOND;
import com.solome.logic.Shapea;
import com.solome.map.MapReader;
import com.solome.ui.SoGif;
import com.solome.ui.SoLableTTF;
import com.solome.ui.SoScrollView;
import com.solome.ui.SoScrollView.SoScrollType;
import com.solome.ui.SoTextField;
import com.solome.util.InvokeFactory;
import com.solome.util.RandomUtils;

public class Main extends AbstractScreen {
	public Main(Game game) {
		super(game);
		main = this;
	}

	Main main;

	int drawSzie = 0;
	// Messages
	public final String MESSAGE = "此外，Apple Watch也可能会拉动iPhone的销量，其将促使消费者购买最新款的iPhone，以提升使用体验。例如，iOS 8系统包含健康应用和HealthKit功能，?这一系统要求iPhone不能太旧，苹果希望至少iPhone 5及更新的设备。根据此前消息，Apple Watch将于4月登陆美国?加拿大及部分欧洲国家，也不排除在中国市场首发的可能?，这款手表的起售价为349美元?";
	public final String sosooss0 = "然后再看看CheckBox。CheckBox的样式定义在CheckBoxStyle中，需要4个参数，两种状态的各一张图片，一个BitmapFont和Color。";
	public final String sosooss1 = "其他的UI大致用法差不多，显示的样式在对应的Style或者Skin中定义。但是要注意有些UI类需要手动设置width和";
	public final String sosooss2 = "然后再看看Button吧，实例化需要一个ButtonStyle，定义了按钮三种状态对应的图片样式，按下和松开时的X,Y偏移还";
	public final String sosooss3 = "友情提示：本赛季第 13 轮您的联赛排名为 115。本赛季结束时，排行在 16384 名之前(含 16384 名)的俱乐部可以参加下届";
	public final String sosooss4 = "神魔之战时，黄帝依九天玄女之兵书布九天玄女战阵，精选将士成立铁血营（后来发展为铁血圣堂）。与之相对，蚩尤亦挑选手下高手组建";
	public final String sosooss5 = "全军覆没，只有数十人死里逃生。修罗道生还兵将从此隐姓埋名，隐居山林。若干年后，当年修罗道的子孙后代重出江湖，以蚩尤所传魔功纵横武林，扬名天下，与名门正派分庭抗礼。世人称之为“修罗魔道”，江湖中";
	public final String sosooss6 = "武当山位于湖北省西北部，乃道教圣地，被誉为“天下第一仙山”，而武当派更与少林派齐名，一道一佛，均为武林中的泰山北斗。武当派的创派祖师张三丰，年少时曾高中举人，但他性情淡薄，不喜";
	public final String sosooss7 = "应龙早已飞升天庭，却留下丹药、符录、道卷无数。张三丰得赐天缘，喜出望外，夜以继日研读洞府中的道书经卷，终有所悟。后开宗立派，广收弟子，武当扬名天下";
	public final String sosooss8 = "幽冥鬼域位于邙山之中，邙山又名鬼山，山中多是葬穴坟墓，阴气极重，且终日云深雾锁，鬼气森森，透不进阳光。幽冥鬼域神秘诡异、高深";
	public final String sosooss9 = "门派特色：幽冥鬼域的球风正如其门派武功一样，诡异狠辣、狡猾灵动，球路难以捉摸。如毒蛇吐信，会冷不防咬上对手一口。 ";
	public final String sosooss10 = "门派特色：唐门秘堡弟子踢球承袭了其暗器武功的神髓，眼观六路，耳听八方，只要对手露出一个微小的漏洞，必抓住机会，一击毙命。";

	Texture texture;
	MapReader mapReader = null;
	public final String[] font = { sosooss0, sosooss1, sosooss2, sosooss3,
			sosooss4, sosooss5, sosooss6, sosooss7, sosooss8, sosooss9,
			sosooss10 };

	@Override
	public void init() {
		// TODO Auto-generated method stub
		// stage = new Stage();
		texture = new Texture("back.png");
		this.setInputAdapter();

		mapReader = new MapReader(this.game);

		Image image = new Image(texture);
		// this.addChild(image, 0);
		Texture textureUp = new Texture("menu/1.png");
		Texture textureDown = new Texture("menu/2.png");

		SpriteDrawable upDraw = new SpriteDrawable(new Sprite(textureUp));
		SpriteDrawable downDraw = new SpriteDrawable(new Sprite(textureDown));

		final Button button = new Button(upDraw, downDraw);
		button.setPosition(0, downDraw.getSprite().getHeight());
		this.add(button);
		button.addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int b) {
				new FontHandler(main).run();
				return super.touchDown(event, x, y, pointer, b);
			}
		});

		Button button1 = new Button(upDraw, downDraw);
		button1.setPosition(upDraw.getSprite().getWidth(), downDraw.getSprite()
				.getHeight());
		this.add(button1);

		// this.remove(1);

		SoGif gif = new SoGif("gif/200.gif", PlayMode.LOOP);
		gif.setPosition(0, downDraw.getSprite().getHeight() * 2);
		this.add(gif);

		// mapReader = new MapReader(game);
		// mapReader.show();

		// InvokeFactory.getInstance().addInvok(new TestInvoke());
		this.factory.addInvok(new TestInvoke());

		this.setDebugGameInfo(true);
		// TextFieldStyle style = new TextFieldStyle();
		// style.font = cache.getFont();//
		// FontFactory.getInstance().getBitmapFont("SoLableTTF_20");
		// style.fontColor = Color.ORANGE;
		// Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
		// TextField field = new TextField("", skin);
		// field.setPosition(400, 0);
		// field.setMessageText("ssssssss");
		// this.addChild(field);

		image = new Image(textureUp);

		SoScrollView scrollView = SoScrollView.create(SoScrollType.Vertical);
		scrollView.addChild(textureUp);
		scrollView.addChild(textureUp);
		scrollView.addChild(textureUp);
		scrollView.addChild(textureUp);
		scrollView.addChild(textureUp);
		scrollView.setBounds(image.getWidth() * 4, 300, image.getWidth(),
				image.getHeight() * 3);
		this.add(scrollView);

		SoTextField field = new SoTextField("", 22, Color.GREEN);
		field.setPosition(400, this.getHeight() - 50);
		field.setMessageText("请输入字符");
		field.setWidth(60);
		field.setHeight(30);
		this.add(field);

		// TextArea area = new TextArea("", style);
		// area.getFirstLineShowing();
		// area.setBounds(300, 100, 200, 200);
		// OnscreenKeyboard keyboard = new OnscreenKeyboard() {
		//
		// @Override
		// public void show(boolean visible) {
		// // TODO Auto-generated method stub
		// System.out.println(".............."+visible);
		// Gdx.input.setOnscreenKeyboardVisible(visible);
		// }
		// };
		// area.setOnscreenKeyboard(keyboard);

		Shapea shapea = new Shapea();
		shapea.setPosition(400, 400);
		this.add(shapea, 100);

		int i = 0;
		SoLableTTF lableTTF = new SoLableTTF(
				sosooss0, 20, Color.BLUE);
		lableTTF.setPosition(0, 320 + (i + drawSzie) * 18);
		this.add(lableTTF, i);
		// i++;
		// lableTTF = new SoLableTTF(sosooss9, 20, Color.BLUE);
		// lableTTF.setFontScale(3);
		// lableTTF.setPosition(0, 360 + (i + drawSzie) * 18);
		// this.addChild(lableTTF, i);
	}

	@SECOND(10)
	public class TestInvoke extends InvokeFactory.Invokable {

		@Override
		public void invoke() throws Exception {
			// TODO Auto-generated method stub
			// System.err.println("............10妙级定时器测试............."
			// + TimeUtils.getCurrTime());
		}

	}

	long maxTime = 0;
	long minTime = 0;

	class FontHandler implements Runnable {
		Main main;

		public FontHandler(Main main) {
			// TODO Auto-generated constructor stub
			this.main = main;
		}

		@Override
		public void run() {
			try {
				int size = font.length;
				Color color = Color.RED;
				for (int i = 0; i < size; i++) {
					String fontName = font[i >= font.length ? RandomUtils
							.getRandom(0, font.length - 1) : i];
					if (i == 1) {
						color = Color.CYAN;
					}
					if (i == 2) {
						color = Color.BLUE;
					}
					if (i == 3) {
						color = Color.ORANGE;
					}
					if (i == 4) {
						color = Color.TEAL;
					}
					if (i == 5) {
						color = Color.GRAY;
					}
					if (i == 6) {
						color = Color.YELLOW;
					}
					if (i == 7) {
						color = Color.MAGENTA;
					}
					if (i == 8) {
						color = Color.MAROON;
					}
					if (i == 9) {
						color = Color.RED;
					}
					SoLableTTF lableTTF = new SoLableTTF(fontName, 17 + i,
							color);
					lableTTF.setPosition(0, 320 + (i + drawSzie) * 18);
					main.add(lableTTF, i);
				}
				drawSzie += size;
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
			}
		}
	}

	public boolean onTouchDown(int screenX, int screenY, int pointer, int button) {
		// Shapea shapea = (Shapea) this.get(100);
		// shapea.setPosition(screenX, screenY);

		// System.err.println("onTouchDown" + screenX + "," + screenY + ","
		// + pointer + "," + button);
		// Image image = (Image) this.get(100);
		// float step = 2;
		// if (image != null) {
		// if (image.getActions().size>0) {
		// image.getActions().clear();
		// }
		// // MoveByAction enemy2MoveByAction = Actions.moveBy(10, 0, 0);//
		// // 向右走动
		// // enemy2MoveByAction.setDuration(duration);
		// // SequenceAction enemy2SequenceAction = Actions
		// // .sequence(enemy2MoveByAction);// 序列化这两个动作
		// // DelayAction enemy2DelayAction = Actions.delay(0.1f,
		// // enemy2SequenceAction);// 延迟2s
		// // image.addAction(Actions.forever(enemy2DelayAction));// 循环动作
		// // image.setOrigin(image.getWidth() / 2f, image.getHeight() / 2f);//
		// // 设置中心点
		// float x = image.getX();
		// float y = image.getY();
		// // 移动操作
		// MoveToAction action1 = Actions.moveTo(x, y+step);
		// // ScaleByAction action2 = Actions.scaleBy(3, 3, 3);
		// // RotateByAction action3 = Actions.rotateBy(90, 3);
		// //
		// // // 还原操作
		// // ScaleToAction action4 = Actions.scaleTo(1, 1);
		// // RotateToAction action5 = Actions.rotateTo(0);
		// // MoveToAction action6 = Actions.moveTo(0, 0);
		//
		// // 顺序执行action
		// SequenceAction sequenceAction = Actions.sequence(action1/*
		// * ,
		// * action2,
		// * action3,
		// * action4,
		// * action5,
		// * action6
		// */);
		// // sequenceAction.setActor(image);
		//
		// // 无限循环三个动作
		// RepeatAction epeatAction = Actions.forever(sequenceAction);
		// image.addAction(epeatAction);
		// }
		return true;
	}

	public boolean onTouchUp(int screenX, int screenY, int pointer, int button) {
		// System.err.println("onTouchUp" + screenX + "," + screenY + ","
		// + pointer + "," + button);

		// ByteArray array = new ByteArray();
		// array.PutInt(1000);
		// array.PutByte(100);
		// HTTP.send(array);
		return true;
	}

	public boolean onTouchMove(int screenX, int screenY, int pointer) {
		// System.err.println("onTouchMove" + screenX + "," + screenY + ","
		// + pointer);
		return true;
	}

	@Override
	public void draw(float delta) {
		// this.getBatch().draw(texture, 100 + 100 * (float) Math.cos(elapsed),
		// 100 + 25 * (float) Math.sin(elapsed));
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void free() {
		// TODO Auto-generated method stub
		texture.dispose();
		System.err.println("clear...............");
	}
}
